package com.ruoyi.crm.async;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.crm.domain.AssignRecord;
import com.ruoyi.crm.domain.vo.ResourceGainVO;
import com.ruoyi.crm.mapper.AssignRecordMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author mr.huang [jie88888822@163.com]
 * @devTime 2022-06-22
 */
@Slf4j
@Component
public class ClueRecordAsync {

	@Resource
	private AssignRecordMapper assignRecordMapper;

	@Async("threadPoolTaskExecutor")
	public void executeClueRecord(ResourceGainVO clueGainVO, LoginUser loginUser){
		try {
			//休眠2s  证明不会影响主线程
			Thread.sleep(2000);
			List<AssignRecord> assignRecords = new ArrayList<>();
			List<Long> ids = clueGainVO.getIds();
			for (int i = 0; i < ids.size(); i++) {
				AssignRecord as = new AssignRecord();
				as.setType("0");
				as.setLatest("1");
				as.setAssignId(ids.get(i));
				as.setCreateTime(DateUtils.getNowDate());
				as.setCreateBy(loginUser.getUsername());
				as.setUserName(loginUser.getUsername());
				as.setUserId(clueGainVO.getUserId().longValue());
				as.setDeptId(loginUser.getDeptId());
				assignRecords.add(as);
			}
			assignRecordMapper.insertAssignRecordList(assignRecords);
			log.info("************新增线索分配记录完成************");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
